<template>
  <div ref="giteeForm">

  </div>
</template>

<script>
/* eslint-disable */
import axios from 'axios';

export default {
  name: 'giteeController',
  data() {
    return {
      API_DOMAIN: 'https://gitee.com/api/v5',
      accessToken: null,
      user: null,
    };
  },
  methods: {
    async getUserInfo() {
      try {
        const response = await axios.get(`${this.API_DOMAIN}/users/${this.user}`, {
          headers: {
            Authorization: `token ${this.accessToken}`
          }
        });
        return response.data;
      } catch (error) {
        console.error(error);
      }
    },

    async getFileInfo(repo, branch, path) {
      try {
        const response = await axios.get(`${this.API_DOMAIN}/repos/${this.user}/${repo}/contents/${path}`, {
          headers: {
            Authorization: `token ${this.accessToken}`
          }
        });
        return response.data;
      } catch (error) {
        console.error(error);
      }
    },

    async getFileContent(repo, branch, path) {
      const fileInfo = await this.getFileInfo(repo, branch, path);
      if (!fileInfo.content) {
        return '{}';
      }
      return this.$base64.decode(fileInfo.content);
    },

    async getJsonFile(repo, branch, path) {
      const fileContent = await this.getFileContent(repo, branch, path);
      return JSON.parse(fileContent);
    },

    async getRepos() {
      try {
        const response = await axios.get(`${this.API_DOMAIN}/user/repos`, {
          headers: {
            Authorization: `token ${this.accessToken}`
          }
        });
        return response.data;
      } catch (error) {
        console.error(error);
      }
    },

    async createRepo(name, description, isPrivate) {
      try {
        const response = await axios.post(`${this.API_DOMAIN}/user/repos`, {
          name,
          description,
          private: isPrivate,
          auto_init: true
        }, {
          headers: {
            Authorization: `token ${this.accessToken}`
          }
        });
        return response.data;
      } catch (error) {
        console.error(error);
      }
    },

    async deleteRepo(repo) {
      try {
        await axios.delete(`${this.API_DOMAIN}/repos/${this.user}/${repo}`, {
          headers: {
            Authorization: `token ${this.accessToken}`
          }
        });
      } catch (error) {
        console.error(error);
      }
    },

    async createFile(repo, branch, path, message, content) {
      try {
        const response = await axios.put(`${this.API_DOMAIN}/repos/${this.user}/${repo}/contents/${path}`, {
          message,
          content: this.$base64.encode(content),
          branch
        }, {
          headers: {
            Authorization: `token ${this.accessToken}`
          }
        });
        return response.data;
      } catch (error) {
        console.error(error);
      }
    },

    async createJsonFile(repo, branch, path, message, jsonContent) {
      return this.createFile(repo, branch, path, message, JSON.stringify(jsonContent));
    },

    async updateFile(repo, branch, path, message, content, sha) {
      try {
        const response = await axios.put(`${this.API_DOMAIN}/repos/${this.user}/${repo}/contents/${path}`, {
          message,
          content: this.$base64.encode(content),
          sha,
          branch
        }, {
          headers: {
            Authorization: `token ${this.accessToken}`
          }
        });
        return response.data;
      } catch (error) {
        console.error(error);
      }
    },

    async updateJsonFile(repo, branch, path, message, jsonContent, sha) {
      return this.updateFile(repo, branch, path, message, JSON.stringify(jsonContent), sha);
    },

    async deleteFile(repo, branch, path, message, sha) {
      try {
        await axios.delete(`${this.API_DOMAIN}/repos/${this.user}/${repo}/contents/${path}`, {
          headers: {
            Authorization: `token ${this.accessToken}`
          },
          data: {
            message,
            sha,
            branch
          }
        });
      } catch (error) {
        console.error(error);
      }
    },

    init(user, accessToken) {
      this.user = user;
      this.accessToken = accessToken;
    }
  }
};

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
